package cn.yuanqiao.archive.wenjianzhengli.utils;

import org.springframework.util.Base64Utils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.unit.DataSize;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;

public class FileFormatUtils {

    public static final String[] IMAGE_FORMAT = {
            "jpg", "jpeg", "png", "tif", "tiff", "psd", "psb", "raw"
    };
    public static final String[] AUDIO_FORMAT = {
            "wma", "mp3", "aac"
    };
    public static final String[] VIDEO_FORMAT = {
            "wav", "avi", "flv", "rmvb", "mvb", "mp4"
    };

    /*
     * 获取文件类型 默认实现是特定于操作系统的
     * */
    public static String getType(File file) throws IOException {
        return getType(file.toPath());
    }

    public static String getType(Path path) throws IOException {
        final String contentType = Files.probeContentType(path);
        if (contentType == null) {
            return "null";
        }
        final String mimeType = contentType.substring(0, contentType.indexOf("/"));
        switch (mimeType.toLowerCase()) {
            // 文本
            case "text":
                return "text";
            // 图片
            case "image":
                return "image";
            //音频
            case "audio":
                return "audio";
            //视频
            case "video":
                return "video";
            // 二进制
            case "application":
                return "bin";
            // 未定义类型
            default:
                return "null";
        }
    }

    public static BasicFileAttributes fileAttributes(Path path) throws IOException {
        return Files.readAttributes(path, BasicFileAttributes.class);
    }

    /*
     * 获取文件创建时间
     * */
    public static FileTime creationTime(Path path) throws IOException {
        return fileAttributes(path).creationTime();
    }

    /*
     * 获取文件访问时间
     * */
    public static FileTime lastAccessTime(Path path) throws IOException {
        return fileAttributes(path).lastAccessTime();
    }

    /*
     * 获取文件修改时间
     * */
    public static FileTime lastModifiedTime(Path path) throws IOException {
        return fileAttributes(path).lastModifiedTime();
    }

    public static long sizeToByte(String size) {
        return DataSize.parse(size).toBytes();
    }

    /*
     * 获取文件大小 b
     * */
    public static String sizeOfByte(File file) {
        return String.valueOf(DataSize.ofBytes(file.length()));
    }

    /*
     * 获取文件大小 kb
     * */
    public static String sizeOfKilobyte(File file) {
        return String.valueOf(DataSize.ofKilobytes(file.length()));
    }

    /*
     * 获取文件大小 mb
     * */
    public static String sizeOfMegabyte(File file) {
        return String.valueOf(DataSize.ofMegabytes(file.length()));
    }

    /*
     * 获取文件大小 gb
     * */
    public static String sizeOfGigabyte(File file) {
        return String.valueOf(DataSize.ofGigabytes(file.length()));
    }

    /*
     * 获取文件大小 tb
     * */
    public static String sizeOfTerabyte(File file) {
        return String.valueOf(DataSize.ofTerabytes(file.length()));
    }

    /*
     * 文件转base64
     * */
    public static String getBase64(File file) throws IOException {
        return Base64Utils.encodeToString(FileCopyUtils.copyToByteArray(file));
    }

    /*
     * 图片文件转base64
     * */
    public static String getBase64PNG(File file) throws IOException {
        return "data:image/png;base64," + getBase64(file);
    }

    /*
     * 图片文件转base64
     * */
    public static String getBase64JPG(File file) throws IOException {
        return "data:image/jpg;base64," + getBase64(file);
    }

   /* public static void main(String[] args) throws IOException {
        //System.out.println(getProcessId());
        //System.out.println(getBase64JPG(new File("D:\\test\\loadfail.jpg")));
        System.out.println(relativize("C:\\Users\\Lenovo\\IdeaProjects\\JYW_DuoMeiTiXiTong\\guns-vip-main\\src\\main\\java\\cn\\yuanqiao\\duomeiti\\util\\FileUtils.java",
                "C:\\Users\\Lenovo\\IdeaProjects\\JYW_DuoMeiTiXiTong"));
    }*/

    public static String relativize(String path, String root) {
        return relativize(Paths.get(path), Paths.get(root));
    }

    public static String relativize(Path path, String root) throws IOException {
        return relativize(path.toRealPath(), Paths.get(root).toRealPath());
    }

    /*
     * 获取path 相对 root 的相对路径
     * */
    public static String relativize(Path path, Path root) {
        return root.relativize(path).toString().replaceAll("\\\\", "/");
    }

    public static String getProcessId() {
        try {
            Class<?> managementFactoryClass = Class.forName("java.lang.management.ManagementFactory");
            Method getRuntimeMXBean = managementFactoryClass.getDeclaredMethod("getRuntimeMXBean");
            Class<?> runtimeMXBeanClass = Class.forName("java.lang.management.RuntimeMXBean");
            Method getName = runtimeMXBeanClass.getDeclaredMethod("getName");
            Object runtimeMXBean = getRuntimeMXBean.invoke(null);
            String name = (String) getName.invoke(runtimeMXBean);
            return name.split("@")[0];
        } catch (Exception e) {
            try {
                return (new File("/proc/self")).getCanonicalFile().getName();
            } catch (IOException id) {
                return "-";
            }
        }
    }
}
